﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;

namespace WebberCross.JavaScriptBridge.JSBridgeDemoApp
{
    public partial class JSBridgeDemoControl : UserControlBase
    {
        public JSBridgeDemoControl()
        {
            InitializeComponent();            
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                // Send a message to the bridge
                DemoMessage msg = new DemoMessage("Hello from Silverlight Application", base._jsClientID.ToString());
                string jsonMsg = JSONHelper.Serialize(msg);
                base.JSBridgeSend(jsonMsg);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        // Override base JSBridgeReceive method
        public override void JSBridgeReceive(string arg)
        {
            try
            {
                // Deserialize message
                DemoMessage msg = JSONHelper.Deserialize<DemoMessage>(arg);

                // Write message to label
                this.lbl3.Text = string.Format("{0}, from JSBridge Client {1}", msg.Message, msg.JSClientID);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}
